package com.dzwww.recommend.rule.common.bo;

import java.io.Serializable;

/**
 * 分页查询-基类
 *
 * @author <a href="mailto:yjx147503@shuwen.com">叶嘉翔</a>
 * @version 1.0
 * @since 2019-03-27 14:57
 */
public class PageQuery<T> implements Serializable {
    private static final int MAX_PAGE_SIZE = 1000;

    /** 当前页 */
    private int current = 1;
    /** 每页显示数 */
    private int size = 20;
    /** 正序排序字段 */
    private String[] ascs;
    /** 逆序排序字段 */
    private String[] descs;

    private T query;

    public PageQuery() {
    }

    public PageQuery(int current, int size) {
        this.current = current;
        this.size = size;
    }

    public int getCurrent() {
        if (this.current <= 0) {
            this.current = 1;
        }

        return this.current;
    }

    public void setCurrent(int current) {
        this.current = current;
    }

    public int getSize() {
        if (this.size > MAX_PAGE_SIZE) {
            this.size = MAX_PAGE_SIZE;
        }

        return this.size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public T getQuery() {
        return this.query;
    }

    public void setQuery(T query) {
        this.query = query;
    }

    public String[] getAscs() {
        return ascs;
    }

    public void setAscs(String[] ascs) {
        this.ascs = ascs;
    }

    public String[] getDescs() {
        return descs;
    }

    public void setDescs(String[] descs) {
        this.descs = descs;
    }

    public static <M> PageQuery<M> defaultPageQuery(M query){
        PageQuery<M> pageQuery = new PageQuery<>();
        pageQuery.setQuery(query);
        return pageQuery;
    }

    public <M> PageQuery<M> toNewQuery(M query){
        PageQuery<M> newPageQuery = new PageQuery<>(this.getCurrent(), this.getSize());
        newPageQuery.setDescs(this.getDescs());
        newPageQuery.setAscs(this.getAscs());
        newPageQuery.setQuery(query);
        return newPageQuery;
    }
}
